package com.fen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fen.common.Result;
import com.fen.pojo.CompanyInfo;

import com.fen.service.CompanyInfoServict;
import com.fen.vo.requset.RequestCompanyInfoOV;
import com.fen.vo.requset.RequestDeleteByids;
import com.fen.vo.response.ResponseCompanyName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: companyinfo表的控制层
 * @Author: lijiafen
 * @Date:  2021/11/1
 * @Time: 17:36
 * @eamil: 2368044269@qq.com
 */
@RestController
public class ConpanyInfoController {

    @Autowired
    private CompanyInfoServict companyInfoServict;

    /**
     * @Description: 查询所有受捐单位
     * @Author: lijiafen
     * @Date:  2021/11/1
     * @Time: 17:36
     * @eamil: 2368044269@qq.com
     */
    @RequestMapping("/select")
    public Result getconpanyinfo()
    {

        return Result.getSuccess().setDate(companyInfoServict.list());
    }
    @RequestMapping("/list")
    public Result list(RequestCompanyInfoOV requestCompanyInfoOV)
    {
        /**
        * @Description: 分页查询所有数据
        * @Author: lijiafen
        * @Date:  2021/11/3
        * @Time: 19:54
        * @eamil: 2368044269@qq.com
        */
        /*Ipage就是一个分页接口*/
        IPage<CompanyInfo>page=companyInfoServict.listCompanyinfo(requestCompanyInfoOV);
        return Result.getSuccess().setMsg("查询成功").setDate(page);
    }
    @RequestMapping("/getByid/{id}")
    public Result getByid(@PathVariable Integer id)
    {
        /*根据id查询受捐单位*/
        return Result.getSuccess().setDate(companyInfoServict.getById(id));
    }

    @RequestMapping("/saveOrUpdate")
    public Result update(CompanyInfo companyInfo)
    {
        /**
        * @Description: 这个是一个修改和新增方法
        * @Author: lijiafen
        * @Date:  2021/11/2
        * @Time: 17:01
        * @eamil: 2368044269@qq.com
        */
        CompanyInfo com = companyInfoServict.getById(companyInfo.getId());
       /* if (com==null)
        {
            companyInfo.setCreateTime(LocalDateTime.now());
        }*/
        if (companyInfoServict.saveOrUpdate(companyInfo))
        {
            /*saveOrUpdate这个方法会判断主键是否为空，如果为空就执行插入如果不为空就修改*/
            return Result.getSuccess().setMsg("修改成功");
        }
        else {
            return Result.getFailure().setMsg("修改成功");
        }
    }
    /*批量删除接口*/
    @RequestMapping("/deleteByids")
    public Result deleteByids(RequestDeleteByids ids)
    {
        List<Integer>list= Arrays.asList(ids.getIds());
         if (companyInfoServict.removeByIds(list))
         {
             return Result.getSuccess().setMsg("删除成功");
         }
         else {
             return Result.getFailure().setMsg("删除失败");
         }
    }
    /*查询所有受捐单位id和名字*/
    @RequestMapping("/selectname")
    public List<ResponseCompanyName> selectname()
    {
        List<ResponseCompanyName> selectname = companyInfoServict.selectname();
        return selectname;
    }
}
